package com.zmc.dynamic.util;

import cn.hutool.core.date.DatePattern;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtil extends cn.hutool.core.date.DateUtil {

    /**
     * 计算相差天数
     * @param startTime
     * @param endTime
     * @return
     */
    public static int toDays(String startTime, String endTime) {
        return toDays(getLocalDate(startTime), getLocalDate(endTime));
    }
    public static int toDays(LocalDate startTime, LocalDate endTime) {
        Duration duration = Duration.between(startTime.atStartOfDay().with(LocalTime.MAX), endTime.atStartOfDay().with(LocalTime.MAX));
        return (int)duration.toDays();
    }

    public static int toDays(LocalDate startTime) {
        return toDays(startTime,LocalDate.now());
    }

    public static int toDays(LocalDateTime startTime) {
        return toDays(startTime,LocalDateTime.now());
    }
    public static int toDays(LocalDateTime startTime, LocalDateTime endTime) {
        Duration duration = Duration.between(startTime, endTime);
        return (int)duration.toDays();
    }
    public static int toMinutes(LocalDateTime endTime) {
        return toMinutes(LocalDateTime.now(), endTime);
    }
    public static int toMinutes(LocalDateTime startTime, LocalDateTime endTime) {
        Duration duration = Duration.between(startTime, endTime);
        return (int)duration.toMinutes();
    }
    public static int toHours(LocalDateTime startTime, LocalDateTime endTime) {
        Duration duration = Duration.between(startTime, endTime);
        return (int)duration.toHours();
    }

    /**
     * 时间格式-yyyy-MM-dd HH:mm:ss
     * @param startTimeStr
     * @param endTimeStr
     * @return
     */
    public static boolean isEffectiveDate(String startTimeStr, String endTimeStr) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(DatePattern.NORM_DATETIME_PATTERN);
        LocalDateTime startTime = LocalDateTime.parse(startTimeStr,dateTimeFormatter);
        LocalDateTime endTime = LocalDateTime.parse(endTimeStr,dateTimeFormatter);
        return isEffectiveDate(LocalDateTime.now(),startTime,endTime);
    }

    public static boolean isEffectiveDate(LocalDateTime startTime, LocalDateTime endTime) {
        return isEffectiveDate(LocalDateTime.now(),startTime,endTime);
    }
    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     * @author Cassius
     */
    public static boolean isEffectiveDate(LocalDateTime nowTime, LocalDateTime startTime, LocalDateTime endTime) {
        if (nowTime.compareTo(startTime) == 0
                || nowTime.compareTo(endTime) == 0) {
            return true;
        }
        if (nowTime.isAfter(startTime) && nowTime.isBefore(endTime)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isInTime(LocalTime startTime,LocalTime endTime){
        LocalTime curTime = LocalTime.now();
        return isEffectiveDate(curTime,startTime,endTime);
    }

    public static boolean isEffectiveDate(LocalTime nowTime, LocalTime startTime, LocalTime endTime) {
        if (nowTime.compareTo(startTime) == 0
                || nowTime.compareTo(endTime) == 0) {
            return true;
        }
        if (nowTime.isAfter(startTime) && nowTime.isBefore(endTime)) {
            return true;
        } else {
            return false;
        }
    }
    /**
     * 判断当前时间是否在[startTime, endTime]区间，注意时间格式要一致
     *
     * @param nowTime 当前时间
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @return
     * @author Cassius
     */
    public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) {
        if (nowTime.getTime() == startTime.getTime()
                || nowTime.getTime() == endTime.getTime()) {
            return true;
        }

        Calendar date = Calendar.getInstance();
        date.setTime(nowTime);

        Calendar begin = Calendar.getInstance();
        begin.setTime(startTime);

        Calendar end = Calendar.getInstance();
        end.setTime(endTime);

        if (date.after(begin) && date.before(end)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isInTime(String sourceTime) {
        String curTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("HH:mm"));
        return isInTime(sourceTime, curTime);
    }

    public static boolean isInTime(LocalDateTime date1,LocalDateTime date2){
        LocalTime startTime = date1.toLocalTime();
        LocalTime endTime = date2.toLocalTime();
        LocalTime curTime = LocalTime.now();
        return isEffectiveDate(curTime,startTime,endTime);
    }

    /**
     * @author Cassius
     * 判断某一时间是否在一个区间内
     * @param sourceTime 时间区间,半闭合 如[21:00-08:00)
     * @param curTime    需要判断的时间 如10:00
     * @return true 在区间内 false 不在区间内
     * @throws IllegalArgumentException
     */
    public static boolean isInTime(String sourceTime, String curTime) {
        if (sourceTime == null || !sourceTime.contains("-") || !sourceTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }
        if (curTime == null || !curTime.contains(":")) {
            throw new IllegalArgumentException("Illegal Argument arg:" + curTime);
        }
        String[] args = sourceTime.split("-");
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
        try {
            long now = sdf.parse(curTime).getTime();
            long start = sdf.parse(args[0]).getTime();
            long end = sdf.parse(args[1]).getTime();
            if ("00:00".equals(args[1])) {
                args[1] = "24:00";
            }
            if (end < start) {
                if (now >= end && now < start) {
                    return false;
                } else {
                    return true;
                }
            } else {
                if (now >= start && now < end) {
                    return true;
                } else {
                    return false;
                }
            }
        } catch (ParseException e) {
            throw new IllegalArgumentException("Illegal Argument arg:" + sourceTime);
        }

    }

    /**
     * 获取当前日期
     * @return
     */
    public static Date getCurrentTime(){
        return Calendar.getInstance().getTime();
    }

    public static Date getDate(String dataStr){
        return getDate(dataStr,DATE_FORMAT_yyyyMMdd_1);
    }

    public static Date getDate(Date date){
        return getDate(getDateStr(date),DATE_FORMAT_yyyyMMdd_1);
    }

    public static Long toEpochMilli(){
        return toEpochMilli(LocalDateTime.now());
    }

    public static Long toEpochMilli(LocalDateTime localDateTime){
        return localDateTime.toInstant(ZoneOffset.systemDefault().getRules().getOffset(LocalDateTime.now())).toEpochMilli();
    }
    public static Long toEpochSecond(){
        return toEpochSecond(LocalDateTime.now());
    }
    public static Long toEpochSecond(LocalDateTime localDateTime){
        return localDateTime.toEpochSecond(ZoneOffset.systemDefault().getRules().getOffset(LocalDateTime.now()));
    }
    public static LocalDateTime getLocalDateTimeS(Long second){
        return LocalDateTime.ofEpochSecond(second, 0, ZoneOffset.systemDefault().getRules().getOffset(LocalDateTime.now()));
    }

    public static LocalDateTime getLocalDateTimeM(Long milliseconds){
        return Instant.ofEpochMilli(milliseconds).atZone(ZoneOffset.systemDefault().getRules().getOffset(LocalDateTime.now())).toLocalDateTime();
    }

    public static String getLocalDateTimeNum(){
        return getLocalDateTimeNum(LocalDateTime.now());
    }

    public static String getLocalDateTimeNum(LocalDateTime localDateTime){
        return getLocalDateTimeStr(localDateTime,DATE_FORMAT_yyyyMMddhhmmss);
    }

    public static LocalDateTime getLocalDateTimeNum(String dataStr){
        return LocalDateTime.parse(dataStr,DateTimeFormatter.ofPattern(DATE_FORMAT_yyyyMMddhhmmss));
    }

    public static LocalDateTime getLocalDateTime(String dataStr){
        return LocalDateTime.parse(dataStr,DateTimeFormatter.ofPattern(DATE_FORMAT_yyyy_MM_dd_hhmmss));
    }

    public static LocalDateTime getLocalDateTime(String dataStr,String pattern){
        return LocalDateTime.parse(dataStr,DateTimeFormatter.ofPattern(pattern));
    }

    public static String getLocalDateTimeStr(LocalDateTime localDateTime,String pattern){
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }
    public static String getLocalDateTimeStr(){
        return getLocalDateTimeStr(LocalDateTime.now());
    }
    public static String getLocalDateTimeStr(LocalDateTime localDateTime){
        return localDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT_yyyy_MM_dd_hhmmss));
    }

    public static String getLocalDateTimeHHStr(LocalDateTime localDateTime){
        return localDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT_yyyyMMdd_2));
    }

    public static String getLocalDateTimeHHMMStr(LocalDateTime localDateTime){
        return localDateTime.format(DateTimeFormatter.ofPattern(DATE_FORMAT_yyyyMMdd_3));
    }

    public static LocalDate getLocalDate(String dataStr){
        return LocalDate.parse(dataStr,DateTimeFormatter.ofPattern(DATE_FORMAT_yyyyMMdd_1));
    }

    public static LocalDate getLocalDate(String dataStr,String pattern){
        return LocalDate.parse(dataStr,DateTimeFormatter.ofPattern(pattern));
    }

    public static String getLocalDateStr(LocalDate localDate,String pattern){
        return localDate.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String getLocalDateStr(LocalDate localDate){
        return localDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT_yyyyMMdd_1));
    }

    public static String getLocalDateY_M_dStr(LocalDate localDate){
        return localDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT_yyyy_MM_dd));
    }

    public static String getLocalDateY_M_dStr(){
        return getLocalDateY_M_dStr(LocalDate.now());
    }

    public static String getLocalDateY_MStr(LocalDate localDate){
        return localDate.format(DateTimeFormatter.ofPattern(DATE_FORMAT_yyyy_MM));
    }

    public static String getLocalDateY_MStr(){
        return getLocalDateY_MStr(LocalDate.now());
    }

    /**
     * 字符串转日期
     * @param dataStr
     * @param pattern
     * @return
     */
    public static Date getDate(String dataStr,String pattern){
        try {
            return new SimpleDateFormat(pattern).parse(dataStr);
        } catch (ParseException e) {
            return null;
        }
    }
    /**
     *将字符串格式yyyyMMdd的字符串转为日期，格式"yyyy-MM-dd"
     *
     * @param dateStr 日期字符串
     * @return 返回格式化的日期
     */
    public static String strToDateFormat(String dateStr)  {
        if("长期".equals(dateStr)) {
            return "9999-01-01";
        }
        Date date = DateUtil.getDate(dateStr, DateUtil.SHORTDATE_FORMAT_STR);
        return DateUtil.format(date, DateUtil.SHORT_DATE_FORMAT_STR);
    }

    public static String getDateStr(Date date){
        return getDateFormatStr(date,DATE_FORMAT_yyyyMMdd_1);
    }

    public static String getDateStrHH(Date date){
        return getDateFormatStr(date,DATE_FORMAT_yyyyMMdd_2);
    }

    public static String getDateStrHHMM(Date date){
        return getDateFormatStr(date,DATE_FORMAT_yyyyMMdd_3);
    }

    public static Date getDateTime(String dateStr){
        return getDate(dateStr,DATE_FORMAT_yyyy_MM_dd_hhmmss);
    }

    /**
     *
     * @param date
     * @return
     */
    public static String getDateTime(Date date){
        return getDateFormatStr(date,DATE_FORMAT_yyyy_MM_dd_hhmmss);
    }

    public static String getDateTimeNum(Date date){
        return getDateFormatStr(date,DATE_FORMAT_yyyyMMddhhmmss);
    }
    /**
     * 获取分钟差时间
     * @return
     */
    public static String getDateStr(int fen){
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, fen);// 5分钟之前的时间
        Date beforeD = beforeTime.getTime();
        return getDateFormatStr(beforeD,LONG_DATE_FORMAT_STR);
    }
    /**
     * 获取分钟差时间
     * @return
     */
    public static String getDateStr(Date date, int fen){
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.setTime(date);
        beforeTime.add(Calendar.MINUTE, fen);// 5分钟之前的时间
        Date beforeD = beforeTime.getTime();
        return getDateFormatStr(beforeD,LONG_DATE_FORMAT_STR);
    }
    /**
     * 获取分钟差时间
     * @return
     */
    public static Date getDate(Date date, int fen){
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.setTime(date);
        beforeTime.add(Calendar.MINUTE, fen);// 5分钟之前的时间
        return beforeTime.getTime();
    }
    /**
     *
     * @param date
     * @param format	按约定格式显示日期字符串
     * @return
     */
    public static String getDateFormatStr(Date date,String format){
        SimpleDateFormat fmt = new SimpleDateFormat(format);
        return fmt.format(date);
    }

    /**
     * 比较两个日期相差的天数
     * @param fDate
     * @param oDate
     * @return
     */
    public static int daysOfTwo(Date fDate, Date oDate) {
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(fDate);

        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(oDate);
        int day1 = cal1.get(Calendar.DAY_OF_YEAR);
        int day2 = cal2.get(Calendar.DAY_OF_YEAR);

        int year1 = cal1.get(Calendar.YEAR);
        int year2 = cal2.get(Calendar.YEAR);
        if (year1 != year2)   //同一年
        {
            int timeDistance = 0;
            for (int i = year1; i < year2; i++) {
                if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0)    //闰年
                {
                    timeDistance += 366;
                } else    //不是闰年
                {
                    timeDistance += 365;
                }
            }

            return timeDistance + (day2 - day1);
        } else    //不同年
        {
            return day2 - day1;
        }
    }
    public static final String DATE_FORMAT_MM = "MM";
    public static final String DATE_FORMAT_DD = "dd";
    public static final String DATE_FORMAT_yyyy_MM = "yyyy_MM";
    public static final String DATE_FORMAT_yyyy_MM_dd = "yyyy_MM_dd";
    public static final String DATE_FORMAT_yyyyMMdd_1 = "yyyy-MM-dd";
    public static final String DATE_FORMAT_yyyyMMdd_2 = "yyyy-MM-dd HH";
    public static final String DATE_FORMAT_yyyyMMdd_3 = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_yyyy_MM_dd_hhmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_yyyyMMddhhmmss = "yyyyMMddHHmmss";
    /**
     * HH:mm:ss
     */
    public static final String HMS_FORMAT_STR = "HH:mm:ss";

    /**
     * yyyy-MM
     */
    public static final String YM_FORMAT_STR = "yyyy-MM";

    /**
     * yyyyMMdd
     */
    public static final String SHORTDATE_FORMAT_STR = "yyyyMMdd";

    /**
     * yyyy.MM.dd
     */
    public static final String SHORTDATE_FORMAT3_STR = "yyyy.MM.dd";

    /**
     * yyyy-MM-dd
     */
    public static final String SHORT_DATE_FORMAT_STR = "yyyy-MM-dd";

    /**
     * yyyy/MM/dd
     */
    public static final String SHORT_DATE_FORMAT2_STR = "yyyy/MM/dd";

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public static final String LONG_DATE_FORMAT_STR = "yyyy-MM-dd HH:mm:ss";

    /**
     * yyyy/MM/dd HH:mm
     */
    public static final String LONG_DATE_FORMAT2_STR = "yyyy/MM/dd HH:mm:ss";

    /**
     * 格式昨天日期 "yyyyMMdd"
     *
     * @return
     */
    public static String FORMAT_YESTERDAY_SHORTDATE() {
        Date date = addDay(new Date(), -1);
        return FORMAT_SHORTDATE(date);
    }

    /**
     * 格式昨天日期 "yyyy-MM-dd"
     *
     * @return
     */
    public static String FORMAT_YESTERDAY_SHORT_DATE() {
        Date date = addDay(new Date(), -1);
        return FORMAT_SHORT_DATE(date);
    }

    /**
     * 格式当前日期 "yyyyMMdd"
     *
     * @param
     * @return
     */
    public static String FORMAT_CURRENT_SHORTDATE() {
        return FORMAT_SHORTDATE(new Date());
    }

    /**
     * 格式当前日期 "yyyy-MM-dd"
     *
     * @param
     * @return
     */
    public static String FORMAT_CURRENT_SHORT_DATE() {
        return FORMAT_SHORT_DATE(new Date());
    }

    /**
     * 格式当前日期 "yyyy-MM-dd HH:mm:ss"
     *
     * @param
     * @return
     */
    public static String FORMAT_CURRENT_LONG_DATE() {
        return FORMAT_LONG_DATE(new Date());
    }

    /**
     * 格式日期 "yyyy-MM"
     *
     * @param date
     * @return
     */
    public static String FORMAT_YMDATE(Date date) {
        return new SimpleDateFormat(YM_FORMAT_STR).format(date);
    }

    /**
     * 格式日期 "yyyyMMdd"
     *
     * @param date
     * @return
     */
    public static String FORMAT_SHORTDATE(Date date) {
        return new SimpleDateFormat(SHORTDATE_FORMAT_STR).format(date);
    }

    /**
     * 格式日期 "yyyy-MM-dd"
     *
     * @param date
     * @return
     */
    public static String FORMAT_SHORT_DATE(Date date) {
        return new SimpleDateFormat(SHORT_DATE_FORMAT_STR).format(date);
    }

    /**
     * 格式日期 "yyyy/MM/dd"
     *
     * @param date
     * @return
     */
    public static String FORMAT_SHORT_DATE2(Date date) {
        return new SimpleDateFormat(SHORT_DATE_FORMAT2_STR).format(date);
    }

    /**
     * 格式日期 "yyyy-MM-dd HH:mm:ss"
     *
     * @param date
     * @return
     */
    public static String FORMAT_LONG_DATE(Date date) {
        return new SimpleDateFormat(LONG_DATE_FORMAT_STR).format(date);
    }

    /**
     * 格式日期 "yyyy/MM/dd HH:mm:ss"
     *
     * @param date
     * @return
     */
    public static String FORMAT_LONG_DATE2(Date date) {
        return new SimpleDateFormat(LONG_DATE_FORMAT2_STR).format(date);
    }

    /**
     * 格式日期 "yyyyMMdd"
     *
     * @param
     * @return
     * @throws ParseException
     */
    public static Date PARSE_SHORTDATE(String dateStr) throws ParseException {
        return new SimpleDateFormat(SHORTDATE_FORMAT_STR).parse(dateStr);
    }

    /**
     * 格式日期 "yyyy-MM-dd"
     *
     * @param
     * @return
     * @throws ParseException
     */
    public static Date PARSE_SHORT_DATE(String dateStr) throws ParseException {
        return new SimpleDateFormat(SHORT_DATE_FORMAT_STR).parse(dateStr);
    }

    /**
     * 格式日期 "yyyy-MM-dd HH:mm:ss"
     *
     * @param
     * @return
     * @throws ParseException
     */
    public static Date PARSE_LONG_DATE(String dateStr) throws ParseException {
        return new SimpleDateFormat(LONG_DATE_FORMAT_STR).parse(dateStr);
    }

    /**
     * 昨天时间
     */
    public static Date Yesterday() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        return calendar.getTime();
    }

    /**
     * 计算 second 秒后的时间
     *
     * @param date
     * @param second
     * @return
     */
    public static Date addSecond(Date date, int second) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.SECOND, second);
        return calendar.getTime();
    }

    /**
     * 计算 minute 分钟后的时间
     *
     * @param date
     * @param minute
     * @return
     */
    public static Date addMinute(Date date, int minute) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MINUTE, minute);
        return calendar.getTime();
    }

    /**
     * 计算 hour 小时后的时间
     *
     * @param date
     * @param
     * @return
     */
    public static Date addHour(Date date, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.HOUR, hour);
        return calendar.getTime();
    }

    /**
     * 计算 day 天后的时间
     *
     * @param date
     * @param day
     * @return
     */
    public static Date addDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_MONTH, day);
        return calendar.getTime();
    }

    /**
     * 计算 month 天后的时间
     *
     * @param date
     * @param month
     * @return
     */
    public static Date addMonth(Date date, int month) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, month);
        return calendar.getTime();
    }

    /**
     * 计算 year 年后的时间
     *
     * @param date
     * @param
     * @return
     */
    public static Date addYear(Date date, int year) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DAY_OF_YEAR, 365 * year);
        return calendar.getTime();
    }

    /**
     * 得到day的起始时间点。
     *
     * @param date
     * @return
     */
    public static Date getDayStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到day的终止时间点.
     *
     * @param date
     * @return
     */
    public static Date getDayEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        calendar.add(Calendar.MILLISECOND, -1);
        return calendar.getTime();
    }

    /**
     * 得到week的起始时间点。
     *
     * @param date
     * @return
     */
    public static Date getWeekStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.get(Calendar.WEEK_OF_YEAR);
        int firstDay = calendar.getFirstDayOfWeek();
        calendar.set(Calendar.DAY_OF_WEEK, firstDay);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到week的终止时间点.
     *
     * @param date
     * @return
     */
    public static Date getWeekEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.get(Calendar.WEEK_OF_YEAR);
        int firstDay = calendar.getFirstDayOfWeek();
        calendar.set(Calendar.DAY_OF_WEEK, 8 - firstDay);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到month的起始时间点。
     *
     * @param date
     * @return
     */
    public static Date getMonthStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到month的终止时间点.
     *
     * @param date
     * @return
     */
    public static Date getMonthEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.MONTH, 1);
        calendar.add(Calendar.MILLISECOND, -1);
        return calendar.getTime();
    }

    /**
     * 得到当前年起始时间
     *
     * @param date
     * @return
     */
    public static Date getYearStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        calendar.set(Calendar.MONTH, 0);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 得到当前年最后一天
     *
     * @param date
     * @return
     */
    public static Date getYearEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
        calendar.set(Calendar.MONTH, 11);
        calendar.set(Calendar.DAY_OF_MONTH, 31);
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 取得月天数
     *
     * @param date
     * @return
     */
    public static int getDayOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return c.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 取得月第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDateOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMinimum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    /**
     * 取得月最后一天
     *
     * @param date
     * @return
     */
    public static Date getLastDateOfMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        return c.getTime();
    }

    /**
     * 取得季度第一天
     *
     * @param date
     * @return
     */
    public static Date getSeasonStart(Date date) {
        return getDayStart(getFirstDateOfMonth(getSeasonDate(date)[0]));
    }

    /**
     * 取得季度最后一天
     *
     * @param date
     * @return
     */
    public static Date getSeasonEnd(Date date) {
        return getDayEnd(getLastDateOfMonth(getSeasonDate(date)[2]));
    }

    /**
     * 取得季度月
     *
     * @param date
     * @return
     */
    public static Date[] getSeasonDate(Date date) {
        Date[] season = new Date[3];

        Calendar c = Calendar.getInstance();
        c.setTime(date);

        int nSeason = getSeason(date);
        if (nSeason == 1) {// 第一季度
            c.set(Calendar.MONTH, Calendar.JANUARY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.FEBRUARY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MARCH);
            season[2] = c.getTime();
        } else if (nSeason == 2) {// 第二季度
            c.set(Calendar.MONTH, Calendar.APRIL);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.MAY);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.JUNE);
            season[2] = c.getTime();
        } else if (nSeason == 3) {// 第三季度
            c.set(Calendar.MONTH, Calendar.JULY);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.AUGUST);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.SEPTEMBER);
            season[2] = c.getTime();
        } else if (nSeason == 4) {// 第四季度
            c.set(Calendar.MONTH, Calendar.OCTOBER);
            season[0] = c.getTime();
            c.set(Calendar.MONTH, Calendar.NOVEMBER);
            season[1] = c.getTime();
            c.set(Calendar.MONTH, Calendar.DECEMBER);
            season[2] = c.getTime();
        }
        return season;
    }

    /**
     *
     * 1 第一季度 2 第二季度 3 第三季度 4 第四季度
     *
     * @param date
     * @return
     */
    public static int getSeason(Date date) {

        int season = 0;

        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH);
        switch (month) {
            case Calendar.JANUARY:
            case Calendar.FEBRUARY:
            case Calendar.MARCH:
                season = 1;
                break;
            case Calendar.APRIL:
            case Calendar.MAY:
            case Calendar.JUNE:
                season = 2;
                break;
            case Calendar.JULY:
            case Calendar.AUGUST:
            case Calendar.SEPTEMBER:
                season = 3;
                break;
            case Calendar.OCTOBER:
            case Calendar.NOVEMBER:
            case Calendar.DECEMBER:
                season = 4;
                break;
            default:
                break;
        }
        return season;
    }

    /**
     * 月的最后一天
     *
     * @param
     * @return
     */
    @SuppressWarnings("deprecation")
    public static Date getLastDayOfMonth(Date date) {
        Calendar cDay1 = Calendar.getInstance();
        cDay1.setTime(date);
        int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
        Date lastDate = cDay1.getTime();
        lastDate.setDate(lastDay);
        return lastDate;
    }

    /**
     * 月的第一天
     *
     * @param date
     * @return
     */
    public static Date getFirstDayOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DATE, calendar.getActualMinimum(Calendar.DATE));
        return calendar.getTime();
    }

}
